/**
 * Supported Browser : MSIE, Chrome , FireFox
 * 
 * Object       : comfunc.js.js
 * Description  : js 파일 설명을 기술합니다.
 * Author       : Kim hyung soo
 * Since        : 2012. 9. 27.
 * Version      : 1.0
 * 
 * Modification Information
 *     since          author              description
 *  ===========    =============    ===========================
 *  Kim hyung soo     2012. 9. 27.     최초 생성
 */

function getBroadcastPagingString(items, currPage, itemsPerPage, pagesPerScreen) {
    var startPage = 0;
    var endPage = 0;
    var prevPaging = 0;
    var nextPaging = 0;
    var totalPages = parseInt((items - 1) / itemsPerPage, 10) + 1;
    
    if (totalPages < 1)
        totalPages = 1;
    
    startPage = parseInt((currPage - 1) / pagesPerScreen, 10) * pagesPerScreen + 1;
    endPage = parseInt((currPage - 1) / pagesPerScreen, 10) * pagesPerScreen + pagesPerScreen;
    prevPaging = startPage - 1;
    nextPaging = endPage + 1;
    
    if (prevPaging < 1)
        prevPaging = 1;
    
    if (nextPaging > totalPages)
        nextPaging = totalPages;

    var pHtml = "";
    
    if (totalPages == 1 || currPage == 1)
        pHtml = "<li class=\"move\"><a href=\"javascript:fn_move_page('prev');\"><img src=\"/hmg/images/web/kor/gbc/groupnews_paging_prev01.png\" alt=\"처음페이지로 이동\" /></a></li>\n";
    else
        pHtml = "<li class=\"move\"><a href=\"javascript:fn_move_page(1);\"><img src=\"/hmg/images/web/kor/gbc/groupnews_paging_prev01.png\" alt=\"처음페이지로 이동\" /></a></li>\n";

    for (var idx = startPage; idx <= endPage && idx <= totalPages; idx++) {
        if (idx == currPage)
            pHtml += "<li class=\"on\"><a href=\"#\">" + idx + "</a></li>\n";
        else
            pHtml += "<li><a href=\"javascript:fn_move_page(" + idx + ");\">" + idx + "</a></li>\n";
    }
    
    if (totalPages == 1 || currPage == totalPages)
        pHtml += "<li class=\"move\"><a href=\"javascript:fn_move_page('next');\"><img src=\"/hmg/images/web/kor/gbc/groupnews_paging_next01.png\" alt=\"마지막페이지로 이동\" /></a></li>\n";
    else
        pHtml += "<li class=\"move\"><a href=\"javascript:fn_move_page(" + totalPages + ");\"><img src=\"/hmg/images/web/kor/gbc/groupnews_paging_next01.png\" alt=\"마지막페이지로 이동\" /></a></li>\n";
    
    return pHtml;
}

function getNewsPagingString(items, currPage, itemsPerPage, pagesPerScreen) {
    var startPage = 0;
    var endPage = 0;
    var prevPaging = 0;
    var nextPaging = 0;
    var totalPages = parseInt((items - 1) / itemsPerPage, 10) + 1;
    
    if (totalPages < 1)
        totalPages = 1;
    
    startPage = parseInt((currPage - 1) / pagesPerScreen, 10) * pagesPerScreen + 1;
    endPage = parseInt((currPage - 1) / pagesPerScreen, 10) * pagesPerScreen + pagesPerScreen;
    prevPaging = startPage - 1;
    nextPaging = endPage + 1;
    
    if (prevPaging < 1)
        prevPaging = 1;
    
    if (nextPaging > totalPages)
        nextPaging = totalPages;

    var pHtml = "";
    
    if (totalPages == 1 || currPage == 1)
        pHtml = "<li class=\"move\"><a href=\"javascript:fn_move_page('prev');\"><img src=\"/hmg/images/web/kor/grn/groupnews_paging_prev01.gif\" alt=\"처음페이지로 이동\" /></a></li>\n";
    else
        pHtml = "<li class=\"move\"><a href=\"javascript:fn_move_page(1);\"><img src=\"/hmg/images/web/kor/grn/groupnews_paging_prev01.gif\" alt=\"처음페이지로 이동\" /></a></li>\n";

    for (var idx = startPage; idx <= endPage && idx <= totalPages; idx++) {
        if (idx == currPage)
            pHtml += "<li class=\"on\"><a href=\"#\">" + idx + "</a></li>\n";
        else
            pHtml += "<li><a href=\"javascript:fn_move_page(" + idx + ");\">" + idx + "</a></li>\n";
    }
    
    if (totalPages == 1 || currPage == totalPages)
        pHtml += "<li class=\"move\"><a href=\"javascript:fn_move_page('next');\"><img src=\"/hmg/images/web/kor/grn/groupnews_paging_next01.gif\" alt=\"마지막페이지로 이동\" /></a></li>\n";
    else
        pHtml += "<li class=\"move\"><a href=\"javascript:fn_move_page(" + totalPages + ");\"><img src=\"/hmg/images/web/kor/grn/groupnews_paging_next01.gif\" alt=\"마지막페이지로 이동\" /></a></li>\n";
    
    return pHtml;
}

function fn_common_move_page(pageNo, src_querystring, currPage, itemsPerPage, totalRows, action) {
    var querystring = src_querystring.substring(1);
    var arrQuerystring = querystring.split("&");
    var resultString = "";
    var totalPages = parseInt((parseInt(totalRows, 10) - 1) / itemsPerPage, 10) + 1;
    
    for (var idx = 0; idx < arrQuerystring.length; idx++) {
        if (arrQuerystring[idx].split("=")[0] != "page" && arrQuerystring[idx].split("=")[0] != "")
            resultString += arrQuerystring[idx] + "&";
    }
        
    if (pageNo == "prev") {
        if (currPage == 1)
            alert("첫 페이지입니다.");
        else
            document.location.href = action + "?" + resultString + "page=" + parseInt(parseInt(currPage, 10) - 1, 10);
    }
    else if (pageNo == "next") {
        if (currPage >= totalPages)
            alert("마지막 페이지입니다.");
        else
            document.location.href = action + "?" + resultString + "page=" + parseInt(parseInt(currPage, 10) + 1, 10);
    }
    else
        document.location.href = action + "?" + resultString + "page=" + pageNo;
}

/* twitter */
function fn_share_twitter(url, title) {
    window.open('http://twitter.com/home?status=' + encodeURIComponent(title) + ' ' + encodeURIComponent(url), 'twitter', '');
}

/* facebook */
function fn_share_facebook(url, title) {
    window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(url) + '&t=' + encodeURIComponent(title), 'facebook', 'width=600,height=250');
}

var defsize = 12;
function fn_font_size(n) {
    resizeFrame ();
    defsize += n;

    if(defsize>20){
        defsize=20;
        alert("더 이상 크게 할 수 없습니다.");
    }else if(defsize<12){
        resizeFrame ();
        defsize=12;
        alert("더 이상 작게 할 수 없습니다");
    }
    else {
        document.getElementsByTagName("body").item(0).style.fontSize=defsize+"px";
        $("p").each(function() {
            var size = parseInt($(this).css("font-size").substring(0, $(this).css("font-size").indexOf("px")), 10) + n;
            $(this).css("font-size", size + "px");
        });
    }
}

function fn_common_search(keyword) {
    if (keyword.length < 2)
        alert("검색어를 2자 이상 입력해 주세요.");
    else
        document.location.href = "/hmg/web/kor/sch/nfd/UnifiedSearch.do?searchKeyword=" + escape(encodeURIComponent(keyword));
}

function fn_common_get_valid_month_string(year, monthList, selected) {
    var optionString = "<option value=\"\">선택</option>\n";

    if (monthList.length > 0) {
        var arrMonthList = monthList.split("/");
        
        for (var idx = 0; idx < arrMonthList.length; idx++) {
            if (year == parseInt(arrMonthList[idx].substring(0, 4), 10)) {
                if (arrMonthList[idx] == selected)
                    optionString += "<option value=\"" + arrMonthList[idx].substring(5, 7) + "\" selected>" + parseInt(arrMonthList[idx].substring(5, 7), 10) + "</option>\n";
                else
                    optionString += "<option value=\"" + arrMonthList[idx].substring(5, 7) + "\">" + parseInt(arrMonthList[idx].substring(5, 7), 10) + "</option>\n";
            }
        }
    }
    
    return optionString;
}

function fn_search_address() {
    popWin("/hmg/web/kor/com/AddressSearch.do", "AddressSearch", 515, 427, 0);
}